<template>
	<view class="box">
		<view class="swiper">
			<view class="swiper-box">
				<swiper autoplay="true" circular="true">
					<swiper-item
						v-for="(item, index) in bannerList"
						:key="index"
					>
						<view class="swiper-item">
							<img :src="item.pic" mode="aspectFill" />
						</view>
					</swiper-item>
				</swiper>
			</view>
		</view>
		<!-- <scroll-view scroll-x="true" class="h-tab" :scroll-left="scroll_left" scroll-with-animation="true">
			<text class="h-tab-item" v-for="(item, index) in categoryList" :key="index" :class="swiperIndex + 1 == index ? 'active' : ''"
				@click="changeTab(item, index)">
				{{item.title}}
			</text>
		</scroll-view> -->
		<div style="padding:20upx">
			<excellentCourseList v-for="(item,i) in courseList" :key="i" class="item" :data="item"></excellentCourseList>
		</div>
	</view>
</template>
<script>
	import excellentCourseList from '@/components/excellent-course-list';
	import {
		get_unifiend_list,
		unified_list
	} from '@/api/product';
	export default {
		components: {
			excellentCourseList,
		},
		data() {
			return {
				id: 0,
				bannerList: [],
				categoryList: [],
				swiperIndex: 0,
				scroll_left: 0,
				tabIndex: 0,
				subject_id: 0,
				courseList: []
			};
		},
		onLoad(options){
		  this.id = options.id
		  this.getBanner()
		},
		methods: {
			onToButtom() {},
			getBanner(){
				this.$http.post(`${unified_list}`, {
					recommend_id: this.id,
					type: 0
				}).then(r => {
					this.bannerList = r.data.banner
					this.categoryList = r.data.cate
					this.subject_id = r.data.cate[0].id
					this.getDetail()
				}).catch((r) => {
				});
			},
			getDetail(){
				this.$http.post(`${get_unifiend_list}`, {
					recommend_id: Number(this.id),
					type: 0,
					subject_id: 0,
					// subject_id: this.subject_id,
					page: 1,
					limit: 20
				}).then(r => {
					this.courseList = r.data.list
				}).catch((r) => {
				});
			},
			changeTab(item,index) {
				this.subject_id = item.id
				this.tabIndex = index - 1; //获取tab索引
				this.swiperIndex = this.tabIndex; //实时更新swiper的索引与tab索引相同
				this.getDetail()
			},
			//滚动到底部事件，因为是设置了scroll-view所以无法使用原生的onReachBottom生命周期函数
			onToButtom() {
				console.log('refresh');
				uni.showLoading({
					title: '加载中',
				});

				setTimeout(() => {
					this.setData();
					uni.hideLoading();
				}, 1000);
			},
			//swiper滑动事件
			changeSwiper(e) {
				let index = e.target.current;
				this.tabIndex = index;
				this.swiperIndex = index;
				let view = uni.createSelectorQuery().select('.h-tab-item');
				view
					.boundingClientRect((data) => {
						this.scroll_left = data.width * index; //根据下标移动tab
					})
					.exec();
				console.log(this.scroll_left);
			}
		},
	};
</script>
<style lang="scss" scoped>
	.swiper {
		width: 100%;
		padding-bottom: 20upx;
		display: flex;
		justify-content: center;
		background: $color-white;
		.swiper-box {
			width: 100%;
			height: 40vw;
			overflow: hidden;
			border-radius: 15upx;
			box-shadow: 0upx 8upx 25upx rgba(0, 0, 0, 0.2);
			position: relative;
			z-index: 1;
			swiper {
				width: 100%;
				height: 40vw;
				swiper-item {
					image {
						width: 100%;
						height: 40vw;
					}
				}
			}
		}
	}
	.box {
		display: flex;
		flex-direction: column;
		// position: fixed;
		// height: 100%;
		// width: 100%;
		// top: 0;
		// left: 0;
	}

	.h-tab {
		white-space: nowrap; //不换行
		width: 100%; //宽度适应屏幕
		height: 80upx;
		line-height: 80upx;
		background-image: -webkit-linear-gradient(left, #2c65f7, #409dff);
		background-image: linear-gradient(to right, #2c65f7, #409dff);
		.h-tab-item {
			margin: 0 $spacing-base;
			font-size: $font-lg;
			color: $color-white;
		}

		.active {
			border-bottom: 1upx solid $color-white;
		}
	}

	.swiper-cont {
		background: $color-white;

		.item {
			display: inline-block;
			width: 50%;
			padding: $spacing-base;
			margin-bottom: $spacing-base;

		}
	}
</style>
